import Api from '~/api'

export class ContactModel {
  public id = 0
  public name = ''
  public avatar?: string
  public phone = ''
  public notes = ''

  // public id: number
  // public name: string
  // public avatar?: string
  // public phone: string
  // public notes: string

  // public constructor() {
  //   this.id = 0
  //   this.name = ''
  //   this.avatar = undefined
  //   this.phone = ''
  //   this.notes = ''
  // }

  static async fromId(id: number): Promise<ContactModel> {
    const contacts = await Api.Contact.getAll()
    const contact = contacts.find(x => x.id === id)
    if (!contact) {
      throw new Error(`ContactModel.fromId(id: ${id}): no such contact`)
    }
    return contact
  }

  uniqueName() {
    return `${this.name}-${this.phone}`
  }
}
